package comgutianyu.qihangjiaoyu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.LinearLayout;
import androidx.fragment.app.Fragment;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import comgutianyu.qihangjiaoyu.R;
import comgutianyu.qihangjiaoyu.activity.LoginActivity;
import comgutianyu.qihangjiaoyu.adapter.CourseAdapter;
import comgutianyu.qihangjiaoyu.model.Course;

public class StudyFragment extends Fragment {
    private TextView tvLoginStatus;
    private LinearLayout contentLayout;
    private RecyclerView rvCourses;
    private CourseAdapter courseAdapter;
    private ActivityResultLauncher<Intent> loginLauncher;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_study, container, false);
        
        initViews(view);
        setupLoginLauncher();
        setupRecyclerView();
        
        return view;
    }

    private void initViews(View view) {
        tvLoginStatus = view.findViewById(R.id.tvLoginStatus);
        contentLayout = view.findViewById(R.id.contentLayout);
        rvCourses = view.findViewById(R.id.rvCourses);

        tvLoginStatus.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            loginLauncher.launch(intent);
        });
    }

    private void setupRecyclerView() {
        rvCourses.setLayoutManager(new LinearLayoutManager(getContext()));
        List<Course> courses = getDummyCourses(); // 获取测试数据
        courseAdapter = new CourseAdapter(courses);
        rvCourses.setAdapter(courseAdapter);
    }

    private List<Course> getDummyCourses() {
        List<Course> courses = new ArrayList<>();
        courses.add(new Course("2025考研英语《句句真研》特课包", "张老师", R.drawable.zhanglaoshi));
        courses.add(new Course("2025考研数学1000题刷题小程序（数学二）", "李老师", R.drawable.lilaoshi));
        courses.add(new Course("2025考研《张宇考研学基础30讲特代》书课包", "王老师", R.drawable.lilaoshi));
        courses.add(new Course("2025考研政治强化班", "张老师", R.drawable.zhanglaoshi));
        courses.add(new Course("2025考研数学进阶课程", "李老师", R.drawable.lilaoshi));
        courses.add(new Course("2025考研英语写作技巧班", "张老师", R.drawable.zhanglaoshi));
        courses.add(new Course("2025考研数学真题解析", "李老师", R.drawable.lilaoshi));
        courses.add(new Course("2025考研英语阅读理解专项", "张老师", R.drawable.zhanglaoshi));
        courses.add(new Course("2025考研数学基础强化", "李老师", R.drawable.lilaoshi));
        courses.add(new Course("2025考研英语词汇精讲", "张老师", R.drawable.zhanglaoshi));
        courses.add(new Course("2025考研数学模拟试题讲解", "李老师", R.drawable.lilaoshi));
        courses.add(new Course("2025考研英语完型填空训练", "张老师", R.drawable.zhanglaoshi));
        courses.add(new Course("2025考研数学考前冲刺班", "李老师", R.drawable.lilaoshi));
        return courses;
    }

    private void setupLoginLauncher() {
        loginLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                updateUI();
            }
        );
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUI();
    }

    private boolean isLoggedIn() {
        return getActivity().getSharedPreferences("user_prefs", getActivity().MODE_PRIVATE)
                .getBoolean("is_logged_in", false);
    }

    private void updateUI() {
        if (isLoggedIn()) {
            tvLoginStatus.setVisibility(View.GONE);
            contentLayout.setVisibility(View.VISIBLE);
        } else {
            tvLoginStatus.setVisibility(View.VISIBLE);
            contentLayout.setVisibility(View.GONE);
        }
    }
} 